package com.cyx.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyx.reggie.common.CustomException;
import com.cyx.reggie.entity.Category;
import com.cyx.reggie.entity.Dish;
import com.cyx.reggie.entity.Employee;
import com.cyx.reggie.entity.Setmeal;
import com.cyx.reggie.mapper.CategoryMapper;
import com.cyx.reggie.mapper.EmployeeMapper;
import com.cyx.reggie.service.CategoryService;
import com.cyx.reggie.service.DishService;
import com.cyx.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 陈彦西
 * @date 2024/7/8 下午3:09
 */
@Service
 public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category>implements CategoryService {
 /**
  * 根据id进行删除分类，删除之前需要进行判断
  * 查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
  * 查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
  */
 @Autowired
 private DishService dishService;

 @Autowired
 private SetmealService setmealService;

 @Override
 @Transactional
 public void remove(Long id){
  LambdaQueryWrapper <Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper();
  dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
  int count1=dishService.count(dishLambdaQueryWrapper);
  if(count1>0){
 throw new CustomException("当前分类下关联了菜品，不能删除");
  }
  LambdaQueryWrapper <Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper();
  setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
  int count2=setmealService.count(setmealLambdaQueryWrapper);
  if(count2>0){
   throw new CustomException("当前分类下关联了套餐，不能删除");
  }
     super.removeById(id);
 }

}